package com.xm.xmapp;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.customview.XLHRatingBar;
import com.xm.xmapp.customview.XLHRatingBar.OnRatingChangeListener;
import com.xm.xmapp.fragment.LongRentOrderFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Submit_order_commentRequest;
import com.xm.xmapp.response.bean.ResponseOrderCommentInfo;
import com.xm.xmapp.response.bean.OrderBeanResponse;
import com.xm.xmapp.tools.ScreenUtils;
import com.xm.xmapp.tools.StringTools;

public class LongRentOrderCommentActivity extends BaseActivity implements OnClickListener {

	private XLHRatingBar order_star, judge_item1, judge_item2, judge_item3, judge_item4, judge_item5;
	private OrderBeanResponse bean;
	private ResponseOrderCommentInfo commentBean;// 评价bean
	private EditText et_content;
	private TextView order_star_text, submit, car_number;
	private TextView judge_text1, judge_text2, judge_text3, judge_text4, judge_text5;
	private Submit_order_commentRequest requestBean = new Submit_order_commentRequest();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_longrentordercomment);
	}

	@Override
	public void initView() {
		order_star = (XLHRatingBar) findViewById(R.id.order_star);
		judge_item1 = (XLHRatingBar) findViewById(R.id.judge_item1);
		judge_item2 = (XLHRatingBar) findViewById(R.id.judge_item2);
		judge_item3 = (XLHRatingBar) findViewById(R.id.judge_item3);
		judge_item4 = (XLHRatingBar) findViewById(R.id.judge_item4);
		judge_item5 = (XLHRatingBar) findViewById(R.id.judge_item5);
		judge_text1 = (TextView) findViewById(R.id.judge_text1);
		judge_text2 = (TextView) findViewById(R.id.judge_text2);
		judge_text3 = (TextView) findViewById(R.id.judge_text3);
		judge_text4 = (TextView) findViewById(R.id.judge_text4);
		judge_text5 = (TextView) findViewById(R.id.judge_text5);
		et_content = (EditText) findViewById(R.id.et_content);
		order_star_text = (TextView) findViewById(R.id.order_star_text);
		submit = (TextView) findViewById(R.id.submit);
		car_number = (TextView) findViewById(R.id.car_number);
	}
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		ScreenUtils.hideSoftKeyboard(this);
		return super.dispatchTouchEvent(ev);
	}

	@Override
	public void initData() {
		setMyTitle("评价");
		commentBean = (ResponseOrderCommentInfo) getIntent().getSerializableExtra("commentBean");
		bean = (OrderBeanResponse) getIntent().getSerializableExtra("bean");
		car_number.setText(bean.car_number);
		if (commentBean == null) {
			requestBean.order_id = bean.order_id;
			int initStar = order_star.getCountSelected();
			order_star_text.setText(StringTools.getCommentText(initStar));
			initStar = judge_item1.getCountSelected();
			judge_text1.setText(StringTools.getCommentText(initStar));
			initStar = judge_item2.getCountSelected();
			judge_text2.setText(StringTools.getCommentText(initStar));
			initStar = judge_item3.getCountSelected();
			judge_text3.setText(StringTools.getCommentText(initStar));
			initStar = judge_item3.getCountSelected();
			judge_text4.setText(StringTools.getCommentText(initStar));
			initStar = judge_item3.getCountSelected();
			judge_text5.setText(StringTools.getCommentText(initStar));
		} else {
			et_content.setEnabled(false);
			findViewById(R.id.submit_layout).setVisibility(View.GONE);
			et_content.setText(commentBean.content);
			order_star.setCountSelected(commentBean.order_star);
			order_star_text.setText(StringTools.getCommentText(commentBean.order_star));
			if (commentBean.judge_item1 == 0) {
				findViewById(R.id.new_layout).setVisibility(View.GONE);
			} else {
				judge_item1.setCountSelected(commentBean.judge_item1);
				judge_item2.setCountSelected(commentBean.judge_item2);
				judge_item3.setCountSelected(commentBean.judge_item3);
				judge_item4.setCountSelected(commentBean.judge_item4);
				judge_item5.setCountSelected(commentBean.judge_item5);

				judge_text1.setText(StringTools.getCommentText(commentBean.judge_item1));
				judge_text2.setText(StringTools.getCommentText(commentBean.judge_item2));
				judge_text3.setText(StringTools.getCommentText(commentBean.judge_item3));
				judge_text4.setText(StringTools.getCommentText(commentBean.judge_item4));
				judge_text5.setText(StringTools.getCommentText(commentBean.judge_item5));
			}
			
		}

	}

	@Override
	public void initEvent() {

		OnRatingChangeListener listener = new OnRatingChangeListener() {

			@Override
			public void onChange(int countSelected, View v) {
				switch (v.getId()) {
				case R.id.order_star:
					order_star_text.setText(StringTools.getCommentText(countSelected));
					break;
				case R.id.judge_item1:
					judge_text1.setText(StringTools.getCommentText(countSelected));
					break;
				case R.id.judge_item2:
					judge_text2.setText(StringTools.getCommentText(countSelected));
					break;
				case R.id.judge_item3:
					judge_text3.setText(StringTools.getCommentText(countSelected));
					break;
				case R.id.judge_item4:
					judge_text4.setText(StringTools.getCommentText(countSelected));
					break;
				case R.id.judge_item5:
					judge_text5.setText(StringTools.getCommentText(countSelected));
					break;
				default:
					break;
				}
				
			}
		};
		order_star.setOnRatingChangeListener(listener);
		judge_item1.setOnRatingChangeListener(listener);
		judge_item2.setOnRatingChangeListener(listener); 
		judge_item3.setOnRatingChangeListener(listener); 
		judge_item4.setOnRatingChangeListener(listener); 
		judge_item5.setOnRatingChangeListener(listener);
		submit.setOnClickListener(this);
	}

	private void submit_order_commentRequest() {

		requestBean.order_star = order_star.getCountSelected();
		requestBean.content = et_content.getText().toString();
		requestBean.judge_item1 = judge_item1.getCountSelected();
		requestBean.judge_item2 = judge_item2.getCountSelected();
		requestBean.judge_item3 = judge_item3.getCountSelected();
		requestBean.judge_item4 = judge_item4.getCountSelected();
		requestBean.judge_item5 = judge_item5.getCountSelected();
		promptDialog.show();
		MyAppcation.getMyAppcation().getPostData(this, requestBean, new GetDataInterFace<String>() {

			@Override
			public void onResponse(String response) {
				promptDialog.dismiss();
				MyAppcation.getMyAppcation().notifyDataSetChanged(LongRentOrderFragment.class,
						Type.F_LongRentOrder.COMMENT_CODE, null);
				MyAppcation.getMyAppcation().notifyDataSetChanged(LongRentOrderDetailsActivity.class,
						Type.A_LongRentOrderDetails.ADDCOMMENT_CODE, null);
				finish();
			}
		}, new DisssmissInterFace() {

			@Override
			public void putError(VolleyError error) {
				promptDialog.dismiss();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			submit_order_commentRequest();
			break;

		default:
			break;
		}
	}

}
